/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.michaelrocks.libphonenumber.ohos.sample;

import io.michaelrocks.libphonenumber.ohos.*;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
/**
 * 测试
 *
 * @author wz
 * @since 2021-04-08
 */
public class ExampleOhosTest {
    private static   PhoneNumberUtil phoneUtil;
    @Test
    public void testBundleName() {
        final String actualBundleName = AbilityDelegatorRegistry.getArguments().getTestBundleName();
        assertEquals("io.michaelrocks.libphonenumber.ohos.sample", actualBundleName);
    }
    @BeforeClass
    public static void set() throws NumberParseException {
        Ability ability = AbilityDelegatorRegistry.getAbilityDelegator().getCurrentTopAbility();
        phoneUtil=  PhoneNumberUtil.createInstance(ability.getContext());
    }
    @Test
    public void testInvalidRegion() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("ZZ");
        assertEquals("+", formatter.inputDigit('+'));
        assertEquals("+4", formatter.inputDigit('4'));
        assertEquals("+48 ", formatter.inputDigit('8'));
        assertEquals("+48 8", formatter.inputDigit('8'));
        assertEquals("+48 88", formatter.inputDigit('8'));
    }
    @Test
    public void testInvalidPlusSign() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("ZZ");
        assertEquals("+", formatter.inputDigit('+'));
        assertEquals("+4", formatter.inputDigit('4'));
        assertEquals("+48 ", formatter.inputDigit('8'));
        assertEquals("+48 8", formatter.inputDigit('8'));
        assertEquals("+48 88", formatter.inputDigit('8'));

    }
    @Test
    public void testTooLongNumberMatchingMultipleLeadingDigits() {
        // See https://github.com/google/libphonenumber/issues/36
        // The bug occurred last time for countries which have two formatting rules with exactly the
        // same leading digits pattern but differ in length.
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("ZZ");
        assertEquals("+", formatter.inputDigit('+'));
        assertEquals("+8", formatter.inputDigit('8'));
        assertEquals("+81 ", formatter.inputDigit('1'));
        assertEquals("+81 9", formatter.inputDigit('9'));
        assertEquals("+81 90", formatter.inputDigit('0'));

    }
    @Test
    public void testCountryWithSpaceInNationalPrefixFormattingRule() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("BY");
        assertEquals("8", formatter.inputDigit('8'));


    }
    @Test
    public void testCountryWithSpaceInNationalPrefixFormattingRuleAndLongNdd() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("BY");
        assertEquals("9", formatter.inputDigit('9'));
        assertEquals("99", formatter.inputDigit('9'));
        assertEquals("999", formatter.inputDigit('9'));
        assertEquals("9999", formatter.inputDigit('9'));

    }
    @Test
    public void testAYTFUS() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("US");
        assertEquals("6", formatter.inputDigit('6'));
        assertEquals("65", formatter.inputDigit('5'));



    }
    @Test
    public void testAYTFUSFullWidthCharacters() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("US");
        assertEquals("\uFF16", formatter.inputDigit('\uFF16'));
        assertEquals("\uFF16\uFF15", formatter.inputDigit('\uFF15'));
        assertEquals("650", formatter.inputDigit('\uFF10'));

    }
    @Test
    public void testAYTFUSMobileShortCode() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("US");
        assertEquals("*", formatter.inputDigit('*'));
        assertEquals("*1", formatter.inputDigit('1'));

    }
    @Test
    public void testAYTFUSVanityNumber() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("US");
        assertEquals("8", formatter.inputDigit('8'));
        assertEquals("80", formatter.inputDigit('0'));

    }
    @Test
    public void testAYTFAndRememberPositionUS() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("US");
        assertEquals("1", formatter.inputDigitAndRememberPosition('1'));


    }
    @Test
    public void testAYTFGBFixedLine() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("GB");
        assertEquals("0", formatter.inputDigit('0'));
        assertEquals("02", formatter.inputDigit('2'));

    }
    @Test
    public void testAYTFGBTollFree() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("GB");
        assertEquals("0", formatter.inputDigit('0'));
        assertEquals("08", formatter.inputDigit('8'));

    }
    @Test
    public void testAYTFGBPremiumRate() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("GB");
        assertEquals("0", formatter.inputDigit('0'));
        assertEquals("09", formatter.inputDigit('9'));
        assertEquals("090", formatter.inputDigit('0'));

    }
    @Test
    public void testAYTFNZMobile() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("NZ");
        assertEquals("0", formatter.inputDigit('0'));
        assertEquals("02", formatter.inputDigit('2'));
        assertEquals("021", formatter.inputDigit('1'));

    }
    @Test
    public void testAYTFDE() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("DE");
        assertEquals("0", formatter.inputDigit('0'));
        assertEquals("03", formatter.inputDigit('3'));


    }
    @Test
    public void testAYTFAR() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("AR");
        assertEquals("0", formatter.inputDigit('0'));
        assertEquals("01", formatter.inputDigit('1'));

    }
    @Test
    public void testAYTFARMobile() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("AR");
        assertEquals("+", formatter.inputDigit('+'));
        assertEquals("+5", formatter.inputDigit('5'));

    }
    @Test
    public void testAYTFKR() {
        // +82 51 234 5678
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("KR");
        assertEquals("+", formatter.inputDigit('+'));
        assertEquals("+8", formatter.inputDigit('8'));


    }
    @Test
    public void testAYTF_MX() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("MX");

        // +52 800 123 4567
        assertEquals("+", formatter.inputDigit('+'));
        assertEquals("+5", formatter.inputDigit('5'));
        assertEquals("+52 ", formatter.inputDigit('2'));



    }
    @Test
    public void testAYTF_International_Toll_Free() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("US");
        // +800 1234 5678
        assertEquals("+", formatter.inputDigit('+'));
        assertEquals("+8", formatter.inputDigit('8'));
        assertEquals("+80", formatter.inputDigit('0'));

    }
    @Test
    public void testAYTFMultipleLeadingDigitPatterns() {
        // +81 50 2345 6789
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("JP");
        assertEquals("+", formatter.inputDigit('+'));
        assertEquals("+8", formatter.inputDigit('8'));
        assertEquals("+81 ", formatter.inputDigit('1'));
        assertEquals("+81 5", formatter.inputDigit('5'));


    }
    @Test
    public void testAYTFLongIDD_AU() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("AU");
        // 0011 1 650 253 2250
        assertEquals("0", formatter.inputDigit('0'));
        assertEquals("00", formatter.inputDigit('0'));
        assertEquals("001", formatter.inputDigit('1'));


    }
    @Test
    public void testAYTFLongIDD_KR() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("KR");
        // 00300 1 650 253 2222
        assertEquals("0", formatter.inputDigit('0'));
        assertEquals("00", formatter.inputDigit('0'));
        assertEquals("003", formatter.inputDigit('3'));

    }
    @Test
    public void testAYTFLongNDD_KR() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("KR");
        // 08811-9876-7890
        assertEquals("0", formatter.inputDigit('0'));
        assertEquals("08", formatter.inputDigit('8'));
        assertEquals("088", formatter.inputDigit('8'));


    }
    @Test
    public void testAYTFLongNDD_SG() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("SG");
        // 777777 9876 7890
        assertEquals("7", formatter.inputDigit('7'));
        assertEquals("77", formatter.inputDigit('7'));
        assertEquals("777", formatter.inputDigit('7'));

    }
    @Test
    public void testAYTFShortNumberFormattingFix_AU() {
        // For Australia, the national prefix is not optional when formatting.
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("AU");

        // 1234567890 - For leading digit 1, the national prefix formatting rule has first group only.
        assertEquals("1", formatter.inputDigit('1'));
        assertEquals("12", formatter.inputDigit('2'));
        assertEquals("123", formatter.inputDigit('3'));

    }
    @Test
    public void testAYTFShortNumberFormattingFix_KR() {
        // For Korea, the national prefix is not optional when formatting, and the national prefix
        // formatting rule doesn't consist of only the first group.
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("KR");

        // 111
        assertEquals("1", formatter.inputDigit('1'));
        assertEquals("11", formatter.inputDigit('1'));
        assertEquals("111", formatter.inputDigit('1'));


    }
    @Test
    public void testAYTFShortNumberFormattingFix_MX() {
        // For Mexico, the national prefix is optional when formatting.
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("MX");

        // 911
        assertEquals("9", formatter.inputDigit('9'));
        assertEquals("91", formatter.inputDigit('1'));
        assertEquals("911", formatter.inputDigit('1'));



    }
    @Test
    public void testAYTFNoNationalPrefix() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("IT");

        assertEquals("3", formatter.inputDigit('3'));
        assertEquals("33", formatter.inputDigit('3'));

    }
    @Test
    public void testAYTFNoNationalPrefixFormattingRule() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("AO");

        assertEquals("3", formatter.inputDigit('3'));
        assertEquals("33", formatter.inputDigit('3'));

    }
    @Test
    public void testAYTFShortNumberFormattingFix_US() {
        // For the US, an initial 1 is treated specially.
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("US");

        // 101 - Test that the initial 1 is not treated as a national prefix.
        assertEquals("1", formatter.inputDigit('1'));
        assertEquals("10", formatter.inputDigit('0'));
        assertEquals("101", formatter.inputDigit('1'));

    }
    @Test
    public void testAYTFClearNDDAfterIDDExtraction() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("KR");

        // Check that when we have successfully extracted an IDD, the previously extracted NDD is
        // cleared since it is no longer valid.
        assertEquals("0", formatter.inputDigit('0'));
        assertEquals("00", formatter.inputDigit('0'));
        assertEquals("007", formatter.inputDigit('7'));
        assertEquals("0070", formatter.inputDigit('0'));
        assertEquals("00700", formatter.inputDigit('0'));


    }
    @Test
    public void testAYTFNumberPatternsBecomingInvalidShouldNotResultInDigitLoss() {
        AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("CN");

        assertEquals("+", formatter.inputDigit('+'));
        assertEquals("+8", formatter.inputDigit('8'));
        assertEquals("+86 ", formatter.inputDigit('6'));
        assertEquals("+86 9", formatter.inputDigit('9'));
        assertEquals("+86 98", formatter.inputDigit('8'));
        assertEquals("+86 988", formatter.inputDigit('8'));

    }
}